package com.clearance.mine_clearance.config;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clearance.mine_clearance.bot.MineClearanceBot;
import com.clearance.mine_clearance.bot.NumTest;
import com.clearance.mine_clearance.entry.*;
import com.clearance.mine_clearance.mapper.*;
import com.clearance.mine_clearance.untils.InviteLinkUntils;
import com.clearance.mine_clearance.untils.RedUntils;
import com.clearance.mine_clearance.untils.RedisUntils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class BotConfig implements ApplicationRunner {
    @Autowired
    private TgUserNapper tgUserNapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ConfigMapper configMapper;

    @Autowired
    private ButtenConfigMapper buttenConfigMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        TelegramBotsApi telegramBotsApi = null;
        try {
            telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
            telegramBotsApi.registerBot(new MineClearanceBot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
        //删除所有的用户信息;
        stringRedisTemplate.delete(Constants.TGUSER+"*");
        LambdaQueryWrapper<TgUser> tgUserNapperLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tgUserNapperLambdaQueryWrapper.eq(TgUser::getType,0);
        List<TgUser> tgUserList = tgUserNapper.selectList(tgUserNapperLambdaQueryWrapper);
        for (TgUser tgUser : tgUserList) {
            log.info("添加用户数据:{}",tgUser);
            stringRedisTemplate.opsForValue().set(Constants.TGUSER+tgUser.getTgId(), JSON.toJSONString(tgUser));
        }
        Config config = configMapper.selectById(1);
        log.info("添加redis配置列表{}",config);
        RedisUntils.setValue(Constants.config,JSON.toJSONString(config));
        log.info("添加按钮配置");
        List<ButtenConfig> buttenConfigList = buttenConfigMapper.selectList(new LambdaQueryWrapper<>());
        RedisUntils.setValue(Constants.buttonConfig,JSON.toJSONString(buttenConfigList));
    }
}

















